﻿using System.Linq;
using System.Web.Mvc;
using NC.ViewModels;
using System.Net.Mail;
using NC.Data;
using NC.Models;
using Subscription = NC.Data.Subscription;

namespace NC.Controllers
{
    public class NepaliCornerController : Controller
    {
        public ActionResult Index()
        {
            InitializeConstants();
            ViewBag.Success = TempData[Resource.ResetSuccessMsg] ?? TempData[Resource.PostSuccessMsg];
            if (Session[Resource.UserId] == null) return View();
            ViewBag.FirstName = Session[Resource.Username];
            TempData[Resource.NCUserID] = Session[Resource.UserId];
            return View();
        }

        public void InitializeConstants()
        {
            NEPALICORNEREntities ncEntities = new NEPALICORNEREntities();
            var firstOrDefault = ncEntities.Countries.FirstOrDefault(x => x.CountryName == "Dummy Country");
            if (firstOrDefault != null)
                NcConstants.DefaultCountryId = firstOrDefault.Id;
            var orDefault = ncEntities.Cities.FirstOrDefault(x => x.CityName == "Dummy City");
            if (orDefault != null)
                NcConstants.DefaultCityId = orDefault.Id;
            var @default = ncEntities.States.FirstOrDefault(x => x.StateName == "Dummy State");
            if (@default != null)
                NcConstants.DefaultStateId = @default.Id;
        }

        public ActionResult ContactUs()
        {
            return View();
        }

        [HttpPost]
        public JsonResult ContactUs(string senderName, string senderAddress, string message, string yourphone)
        {
            var mail = new MailMessage(NcConstants.NcEmail, "info@nepalicorner.com")
            {
                Body = string.Format("Sender Name: " + senderName + " SenderAddress: " + senderAddress + "Sender Phone: " + yourphone + "Message: " + message),
                Subject = "Contact Us Message",
                IsBodyHtml = true,
                BodyEncoding = System.Text.Encoding.GetEncoding("utf-8")
            };
            var email = new Email(mail);
            return Json(email.Send() ? new { message = Resource.SuccessMsg} : new { message = Resource.FailureMsg});
        }
        
        [HttpPost]
        public ActionResult RegisterSubscription(Subcribe sub)
        {
            if (!ModelState.IsValid) return null;
            Subscription subsc = new Subscription {SubscriberEmail = sub.SubscriptionEmailId, StateId = sub.StateId};
            return null;
        }

        [HttpPost]
        public void DisplayUsersLocation()
        {

        }
    }
}